package com.github.llyb120.sxmanage;

import cn.hutool.core.collection.ConcurrentHashSet;
import cn.hutool.core.util.IdUtil;
import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

/**
 * @Author: Administrator
 * @Date: 2020/12/24 14:07
 */
@CrossOrigin
@RequestMapping
@RestController
public class LoginController {

    public static Set<String> tokens = new ConcurrentHashSet<>();

    @Value("${login.username:}")
    String username;
    @Value("${login.password:}")
    String password;

    @RequestMapping("login")
    public String login(
        String username,
        String password
    ){
        String token;
        if(username.equals(this.username) && password.equals(this.password)){
            tokens.add(token = IdUtil.objectId());
            return token;
        }
        return "failed";
    }


    @RequestMapping("checkLogin")
    public boolean checkLogin(String token){
        if (token == null) {
            return false;
        }
        return tokens.contains(token);
    }
}
